﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using DNA.Utility;

namespace DNA.Mvc.Data.Xml
{
    public class EventScoreRepository : IEventScoreRepository
    {
        private List<EventScore> dbSet = new List<EventScore>();
        private string fileName;

        public EventScoreRepository(string filename)
        {
            if (filename.StartsWith("~/"))
                fileName = HttpContext.Current.Server.MapPath(filename);

            var scores = XmlSerializerUtility.DeserializeFormXmlFile<ScoreElement>(fileName);
             if (scores.Events != null)
                 dbSet.AddRange(scores.Events);
        }

        public IQueryable<EventScore> All()
        {
            return dbSet.AsQueryable();
        }

        public IQueryable<EventScore> All(out int total, int index = 0, int size = 50)
        {
            var skip = index * size;
            total = dbSet.Count;

            if (skip == 0)
                return dbSet.Take(size).AsQueryable();
            else
                return dbSet.Skip(skip).Take(size).AsQueryable();
        }

        public IQueryable<EventScore> Filter(System.Linq.Expressions.Expression<Func<EventScore, bool>> predicate)
        {
            return dbSet.Where(predicate.Compile()).AsQueryable();
        }

        public IQueryable<EventScore> Filter<Key>(System.Linq.Expressions.Expression<Func<EventScore, Key>> sortingSelector, System.Linq.Expressions.Expression<Func<EventScore, bool>> filter, out int total, SortingOrders sortby = SortingOrders.Asc, int index = 0, int size = 50)
        {
            throw new NotImplementedException();
        }

        public bool Contains(System.Linq.Expressions.Expression<Func<EventScore, bool>> predicate)
        {
            return dbSet.Count(predicate.Compile()) > 0;
        }

        public int Count()
        {
            return dbSet.Count;
        }

        public int Count(System.Linq.Expressions.Expression<Func<EventScore, bool>> predicate)
        {
            return dbSet.Count(predicate.Compile());
        }

        public EventScore Find(params object[] keys)
        {
            var name = keys[0] as string;
            return dbSet.FirstOrDefault(d => d.Name.Equals(name));
        }

        public EventScore Find(System.Linq.Expressions.Expression<Func<EventScore, bool>> predicate)
        {
            return dbSet.FirstOrDefault(predicate.Compile());
        }

        public EventScore Create(EventScore t)
        {
            dbSet.Add(t);
            Save();
            return t;
        }

        public void Delete(EventScore t)
        {
            dbSet.Remove(t);
            Save();
        }

        public int Delete(System.Linq.Expressions.Expression<Func<EventScore, bool>> predicate)
        {
            throw new NotImplementedException();
        }

        public EventScore Update(EventScore t)
        {
            dbSet.Remove(t);
            dbSet.Add(t);
            Save();
            return t;
        }

        private void Save()
        {
            var scores=new ScoreElement();
            scores.Events=dbSet;
            XmlSerializerUtility.SerializeToXmlFile<ScoreElement>(fileName, scores);
        }

        public void Dispose()
        {
            dbSet.Clear();
            GC.SuppressFinalize(this);
        }


        public int Submit()
        {
            Save();
            return dbSet.Count;
        }
    }
}
